package com.example.t8v19window;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;

public class HelloController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button setSizeBtn;

    @FXML
    private Button solveBtn;

    @FXML
    private TextField xTF;

    @FXML
    private TextField yTF;
    @FXML
    private GridPane arrPane;
    @FXML
    private Label resLabel;
    private boolean[][] arr;

    @FXML
    void setSizeClick(ActionEvent event) {
        arrPane.getChildren().clear();
        int x = Integer.parseInt(xTF.getText()), y = Integer.parseInt(yTF.getText());
        this.arr = new boolean[x][y];
        int i, j;
        for (i = 0; i < y; i++) {
            for (j = 0; j < x; j++) {
                Button btn = new Button();
                btn.setBackground(Background.fill(Color.WHITE));
                btn.setText("0");
                final int ii = i, jj = j;
                btn.setOnAction(e -> {
                    if (btn.getText().equals("0")) {
                        btn.setText("1");
                        arr[jj][ii] = true;
                    } else {
                        btn.setText("0");
                        arr[jj][ii] = false;
                    }
                });
                arrPane.add(btn, i, j);
            }
        }
    }

    @FXML
    void solveClick(ActionEvent event) {
        solve(arr);
    }

    void solve(boolean[][] arr) {
        int height = arr.length, weight = arr[0].length;
        int[][] tver = new int[height][weight];
        int[][] tgor = new int[height][weight];
        int t = 0, t1 = 0, vmax = 0, gmax = 0;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < weight; j++) {
                t = 0;
                t1 = 0;
                if (arr[i][j]) {
                    t = 1;
                    t += i > 0 ? tver[i - 1][j] : 0;
                    t1 = 1;
                    t1 += j > 0 ? tgor[i][j - 1] : 0;
                }
                if (vmax < t) {
                    vmax = t;
                }
                if (gmax < t1) {
                    gmax = t1;
                }
                tver[i][j] = t;
                tgor[i][j] = t1;
                System.out.print(t + " ");
            }
            System.out.println("");
        }
        System.out.println();
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < weight; j++) {
                System.out.print(tgor[i][j] + " ");
            }
            System.out.println();
        }
        int rx = 0, ry = 0, max = 0, w = 0, h = 0;
        boolean rch = false;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < weight; j++) {
                if (tver[i][j] > 0) {
                    if (tver[i][j] > 0) {
                        rch = false;
                        rch = check(tver,tgor,j,i,tver[i][j],tgor[i][j]);
                        if (rch) {
                            if (tver[i][j] * tgor[i][j] > max && tver[i][j] != tgor[i][j]) {
                                h = tver[i][j];
                                w = tgor[i][j];
                                rx = j - w + 1;
                                ry = i - h + 1;
                                max = tver[i][j] * tgor[i][j];
                            }
                        }
                    }
                }
            }
        }
        resLabel.setText("\n\n" + ry + " " + rx + " " + h + " " + w);
    }

    boolean check(int[][] tver, int[][] tgor, int x, int y, int h, int w) {
        boolean tv = true, tg = true;
        int xm = x - w, ym = y - h;
        xm = xm < 0 ? 0 : xm + 1;
        ym = ym < 0 ? 0 : ym + 1;
        if(h == w)
            return false;
        for (int i = ym; i <= y; i++) {
            for (int j = xm; j < x; j++) {
                if (!(tver[i][j] == tver[i][j + 1])) {
                    return false;
                }
            }
        }
        for (int i = xm; i <= x; i++) {
            for (int j = ym; j < y; j++) {
                if (!(tgor[j][i] == tgor[j + 1][i])) {
                    return false;
                }
            }
        }
        if (ym - 1 >= 0 && xm - 1 == 0)
            if (tver[ym - 1][xm - 1] > 0)
                return false;
        if (ym - 1 >= 0 && x + 1 <= arr[0].length - 1)
            if (tver[ym - 1][x + 1] > 0)
                return false;
        if (y + 1 <= arr.length - 1 && xm - 1 >= 0)
            if (tver[y + 1][xm - 1] > 0)
                return false;
        if (y + 1 <= arr.length - 1 && x + 1 <= arr[0].length - 1)
            if (tver[y + 1][x + 1] > 0)
                return false;
        if (xm - 1 >= 0) {
            for (int i = ym; i <= y; i++) {
                if (tver[i][xm - 1] > 0)
                    return false;
            }
        }
        if (x + 1 <= arr[0].length - 1) {
            for (int i = ym; i <= y; i++) {
                if (tver[i][x + 1] > 0)
                    return false;
            }
        }
        if(ym - 1 >= 0){
            for(int i = xm; i <= x; i++){
                if(tver[ym - 1][i] > 0)
                    return false;
            }
        }
        if(y + 1 <= arr.length - 1){
            for(int i = xm; i <= x; i++){
                if(tver[y + 1][i] > 0)
                    return false;
            }
        }
        return true;
    }

    @FXML
    void initialize() {
        assert setSizeBtn != null : "fx:id=\"setSizeBtn\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert solveBtn != null : "fx:id=\"solveBtn\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert xTF != null : "fx:id=\"xTF\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert yTF != null : "fx:id=\"yTF\" was not injected: check your FXML file 'hello-view.fxml'.";

    }

}